/**
 * 签名工具（微信小程序版本）
 * 注意：不要格式化此文件
 */

// MD5加密实现
const _0x4a2b = ['charCodeAt', 'length', 'toString', 'floor', 'abs', 'pow'];
const _0x3c4d = function(_0x1a2b3c: number) {
  return _0x4a2b[_0x1a2b3c];
};

// 内部MD5实现（混淆）
function _0xmd5(_0x5e4a1b: string): string {
  function _0x2e4a(_0x4b2c: number, _0x3a1d: number) {
    return (_0x4b2c << _0x3a1d) | (_0x4b2c >>> (32 - _0x3a1d));
  }
  function _0x1c3e(_0x2d4b: number, _0x5a3c: number) {
    let _0x4e2a, _0x3b1d, _0x5c4e, _0x2a3b, _0x4d5e;
    _0x5c4e = (_0x2d4b & 0x80000000);
    _0x2a3b = (_0x5a3c & 0x80000000);
    _0x4e2a = (_0x2d4b & 0x40000000);
    _0x3b1d = (_0x5a3c & 0x40000000);
    _0x4d5e = (_0x2d4b & 0x3fffffff) + (_0x5a3c & 0x3fffffff);
    if (_0x4e2a & _0x3b1d) return (_0x4d5e ^ 0x80000000 ^ _0x5c4e ^ _0x2a3b);
    if (_0x4e2a | _0x3b1d) {
      if (_0x4d5e & 0x40000000) return (_0x4d5e ^ 0xc0000000 ^ _0x5c4e ^ _0x2a3b);
      else return (_0x4d5e ^ 0x40000000 ^ _0x5c4e ^ _0x2a3b);
    } else return (_0x4d5e ^ _0x5c4e ^ _0x2a3b);
  }
  function _0x3f4a(_0x1a: number, _0x2b: number, _0x3c: number) {
    return (_0x1a & _0x2b) | ((~_0x1a) & _0x3c);
  }
  function _0x4g5b(_0x1a: number, _0x2b: number, _0x3c: number) {
    return (_0x1a & _0x3c) | (_0x2b & (~_0x3c));
  }
  function _0x5h6c(_0x1a: number, _0x2b: number, _0x3c: number) {
    return (_0x1a ^ _0x2b ^ _0x3c);
  }
  function _0x6i7d(_0x1a: number, _0x2b: number, _0x3c: number) {
    return (_0x2b ^ (_0x1a | (~_0x3c)));
  }
  function _0x7j8e(_0xa: number, _0xb: number, _0xc: number, _0xd: number, _0xe: number, _0xf: number, _0xg: number) {
    _0xa = _0x1c3e(_0xa, _0x1c3e(_0x1c3e(_0x3f4a(_0xb, _0xc, _0xd), _0xe), _0xg));
    return _0x1c3e(_0x2e4a(_0xa, _0xf), _0xb);
  }
  function _0x8k9f(_0xa: number, _0xb: number, _0xc: number, _0xd: number, _0xe: number, _0xf: number, _0xg: number) {
    _0xa = _0x1c3e(_0xa, _0x1c3e(_0x1c3e(_0x4g5b(_0xb, _0xc, _0xd), _0xe), _0xg));
    return _0x1c3e(_0x2e4a(_0xa, _0xf), _0xb);
  }
  function _0x9l0g(_0xa: number, _0xb: number, _0xc: number, _0xd: number, _0xe: number, _0xf: number, _0xg: number) {
    _0xa = _0x1c3e(_0xa, _0x1c3e(_0x1c3e(_0x5h6c(_0xb, _0xc, _0xd), _0xe), _0xg));
    return _0x1c3e(_0x2e4a(_0xa, _0xf), _0xb);
  }
  function _0xam1h(_0xa: number, _0xb: number, _0xc: number, _0xd: number, _0xe: number, _0xf: number, _0xg: number) {
    _0xa = _0x1c3e(_0xa, _0x1c3e(_0x1c3e(_0x6i7d(_0xb, _0xc, _0xd), _0xe), _0xg));
    return _0x1c3e(_0x2e4a(_0xa, _0xf), _0xb);
  }
  function _0xbn2i(_0x4a: number) {
    let _0x1b = '';
    let _0x2c, _0x3d;
    for (_0x2c = 0; _0x2c <= 3; _0x2c++) {
      _0x3d = (_0x4a >>> (_0x2c * 8)) & 255;
      _0x3d = '0' + _0x3d[_0x3c4d(2)](16);
      _0x1b = _0x1b + _0x3d.substr(_0x3d[_0x3c4d(1)] - 2, 2);
    }
    return _0x1b;
  }
  let _0x1e: number[] = [];
  let _0x2f, _0x3g, _0x4h, _0x5i, _0x6j, _0xa = 1732584193, _0xb = -271733879, _0xc = -1732584194, _0xd = 271733878;
  _0x5e4a1b = _0xco3j(_0x5e4a1b);
  _0x1e = _0xdp4k(_0x5e4a1b);
  for (_0x2f = 0; _0x2f < _0x1e[_0x3c4d(1)]; _0x2f += 16) {
    _0x3g = _0xa; _0x4h = _0xb; _0x5i = _0xc; _0x6j = _0xd;
    _0xa = _0x7j8e(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 0], 7, -680876936);
    _0xd = _0x7j8e(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 1], 12, -389564586);
    _0xc = _0x7j8e(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 2], 17, 606105819);
    _0xb = _0x7j8e(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 3], 22, -1044525330);
    _0xa = _0x7j8e(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 4], 7, -176418897);
    _0xd = _0x7j8e(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 5], 12, 1200080426);
    _0xc = _0x7j8e(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 6], 17, -1473231341);
    _0xb = _0x7j8e(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 7], 22, -45705983);
    _0xa = _0x7j8e(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 8], 7, 1770035416);
    _0xd = _0x7j8e(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 9], 12, -1958414417);
    _0xc = _0x7j8e(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 10], 17, -42063);
    _0xb = _0x7j8e(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 11], 22, -1990404162);
    _0xa = _0x7j8e(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 12], 7, 1804603682);
    _0xd = _0x7j8e(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 13], 12, -40341101);
    _0xc = _0x7j8e(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 14], 17, -1502002290);
    _0xb = _0x7j8e(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 15], 22, 1236535329);
    _0xa = _0x8k9f(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 1], 5, -165796510);
    _0xd = _0x8k9f(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 6], 9, -1069501632);
    _0xc = _0x8k9f(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 11], 14, 643717713);
    _0xb = _0x8k9f(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 0], 20, -373897302);
    _0xa = _0x8k9f(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 5], 5, -701558691);
    _0xd = _0x8k9f(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 10], 9, 38016083);
    _0xc = _0x8k9f(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 15], 14, -660478335);
    _0xb = _0x8k9f(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 4], 20, -405537848);
    _0xa = _0x8k9f(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 9], 5, 568446438);
    _0xd = _0x8k9f(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 14], 9, -1019803690);
    _0xc = _0x8k9f(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 3], 14, -187363961);
    _0xb = _0x8k9f(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 8], 20, 1163531501);
    _0xa = _0x8k9f(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 13], 5, -1444681467);
    _0xd = _0x8k9f(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 2], 9, -51403784);
    _0xc = _0x8k9f(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 7], 14, 1735328473);
    _0xb = _0x8k9f(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 12], 20, -1926607734);
    _0xa = _0x9l0g(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 5], 4, -378558);
    _0xd = _0x9l0g(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 8], 11, -2022574463);
    _0xc = _0x9l0g(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 11], 16, 1839030562);
    _0xb = _0x9l0g(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 14], 23, -35309556);
    _0xa = _0x9l0g(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 1], 4, -1530992060);
    _0xd = _0x9l0g(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 4], 11, 1272893353);
    _0xc = _0x9l0g(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 7], 16, -155497632);
    _0xb = _0x9l0g(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 10], 23, -1094730640);
    _0xa = _0x9l0g(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 13], 4, 681279174);
    _0xd = _0x9l0g(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 0], 11, -358537222);
    _0xc = _0x9l0g(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 3], 16, -722521979);
    _0xb = _0x9l0g(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 6], 23, 76029189);
    _0xa = _0x9l0g(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 9], 4, -640364487);
    _0xd = _0x9l0g(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 12], 11, -421815835);
    _0xc = _0x9l0g(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 15], 16, 530742520);
    _0xb = _0x9l0g(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 2], 23, -995338651);
    _0xa = _0xam1h(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 0], 6, -198630844);
    _0xd = _0xam1h(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 7], 10, 1126891415);
    _0xc = _0xam1h(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 14], 15, -1416354905);
    _0xb = _0xam1h(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 5], 21, -57434055);
    _0xa = _0xam1h(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 12], 6, 1700485571);
    _0xd = _0xam1h(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 3], 10, -1894986606);
    _0xc = _0xam1h(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 10], 15, -1051523);
    _0xb = _0xam1h(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 1], 21, -2054922799);
    _0xa = _0xam1h(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 8], 6, 1873313359);
    _0xd = _0xam1h(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 15], 10, -30611744);
    _0xc = _0xam1h(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 6], 15, -1560198380);
    _0xb = _0xam1h(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 13], 21, 1309151649);
    _0xa = _0xam1h(_0xa, _0xb, _0xc, _0xd, _0x1e[_0x2f + 4], 6, -145523070);
    _0xd = _0xam1h(_0xd, _0xa, _0xb, _0xc, _0x1e[_0x2f + 11], 10, -1120210379);
    _0xc = _0xam1h(_0xc, _0xd, _0xa, _0xb, _0x1e[_0x2f + 2], 15, 718787259);
    _0xb = _0xam1h(_0xb, _0xc, _0xd, _0xa, _0x1e[_0x2f + 9], 21, -343485551);
    _0xa = _0x1c3e(_0xa, _0x3g); _0xb = _0x1c3e(_0xb, _0x4h);
    _0xc = _0x1c3e(_0xc, _0x5i); _0xd = _0x1c3e(_0xd, _0x6j);
  }
  return (_0xbn2i(_0xa) + _0xbn2i(_0xb) + _0xbn2i(_0xc) + _0xbn2i(_0xd))[_0x3c4d(2)]().toLowerCase();
}

function _0xco3j(_0x1s: string) {
  let _0x2t = '';
  for (let _0x3u = 0; _0x3u < _0x1s[_0x3c4d(1)]; _0x3u++) {
    let _0x4v = _0x1s[_0x3c4d(0)](_0x3u);
    if (_0x4v < 128) _0x2t += String.fromCharCode(_0x4v);
    else if (_0x4v > 127 && _0x4v < 2048) {
      _0x2t += String.fromCharCode((_0x4v >> 6) | 192);
      _0x2t += String.fromCharCode((_0x4v & 63) | 128);
    } else {
      _0x2t += String.fromCharCode((_0x4v >> 12) | 224);
      _0x2t += String.fromCharCode(((_0x4v >> 6) & 63) | 128);
      _0x2t += String.fromCharCode((_0x4v & 63) | 128);
    }
  }
  return _0x2t;
}

function _0xdp4k(_0x1w: string) {
  let _0x2x: number[] = [];
  let _0x3y = (1 << 8) - 1;
  for (let _0x4z = 0; _0x4z < _0x1w[_0x3c4d(1)] * 8; _0x4z += 8) {
    _0x2x[_0x4z >> 5] |= (_0x1w[_0x3c4d(0)](_0x4z / 8) & _0x3y) << (_0x4z % 32);
  }
  _0x2x[_0x1w[_0x3c4d(1)] * 8 >> 5] |= 128 << ((_0x1w[_0x3c4d(1)] * 8) % 32);
  _0x2x[((((_0x1w[_0x3c4d(1)] * 8) + 64) >>> 9) << 4) + 14] = _0x1w[_0x3c4d(1)] * 8;
  return _0x2x;
}

// 签名生成核心
const _0xsig = {
  // 混淆配置矩阵（多层编码）
  _0xa1: [0x64,0x61,0x5f,0x6c,0x61,0x6f,0x5f,0x62,0x69,0x65,0x5f,0x7a,0x61,0x69,0x5f,0x73,0x68,0x75,0x61,0x5f,0x6a,0x69,0x65,0x5f,0x6b,0x6f,0x75,0x5f,0x6c,0x65],
  _0xa2: [0x77,0x6f,0x5f,0x64,0x65,0x5f,0x6a,0x69,0x65,0x5f,0x6b,0x6f,0x75,0x5f,0x62,0x61,0x6f,0x5f,0x6c,0x65],
  _0xa3: [0x73,0x68,0x6f,0x75,0x5f,0x78,0x69,0x61,0x5f,0x31,0x69,0x75,0x5f,0x71,0x69,0x6e,0x67],
  _0xb1: [115,97,108,116],
  _0xb2: [115,97,108,116,95,118,50],
  _0xb3: [115,108,97,116,95,118,51],
  _0xc1: 0x1a2b,
  _0xc2: 0x3c4d,
  _0xc3: 0x5e6f,
  
  // 位运算解码器
  _0xdec1(_0xarr: number[]) {
    return _0xarr.map(_0xv => String.fromCharCode(_0xv ^ 0x0)).join('');
  },
  
  // 逆序解码器
  _0xdec2(_0xarr: number[]) {
    let _0xr = '';
    for (let _0xi = _0xarr.length - 1; _0xi >= 0; _0xi--) {
      _0xr = String.fromCharCode(_0xarr[_0xi]) + _0xr;
    }
    return _0xr;
  },
  
  // 标准解码器
  _0xdec3(_0xarr: number[]) {
    return _0xarr.reduce((_0xacc, _0xval) => _0xacc + String.fromCharCode(_0xval), '');
  },
  
  // 混淆密钥提取器
  _0xextK() {
    const _0xm = this._0xc1 ^ this._0xc2 ^ this._0xc3;
    return [
      this._0xdec1(this._0xb1),
      this._0xdec3(this._0xb2),
      this._0xdec2(this._0xb3)
    ];
  },
  
  // 混淆值提取器
  _0xextV() {
    const _0xt1 = this._0xa1.slice(0);
    const _0xt2 = this._0xa2.slice(0);
    const _0xt3 = this._0xa3.slice(0);
    return [
      this._0xdec1(_0xt1),
      this._0xdec3(_0xt2),
      this._0xdec2(_0xt3)
    ];
  },
  
  // 生成签名
  _0xgen(_0xdata: Record<string, string>) {
    const _0xt = Date.now()[_0x3c4d(2)]();
    const _0xp = { ..._0xdata, timestamp: _0xt };
    
    // 提取密钥和值
    const _0xkeys = this._0xextK();
    const _0xvals = this._0xextV();
    
    // 构建参数对象
    const _0xall: Record<string, string> = { ..._0xp };
    for (let _0xi = 0; _0xi < _0xkeys.length; _0xi++) {
      _0xall[_0xkeys[_0xi]] = _0xvals[_0xi];
    }
    
    // 排序并拼接
    const _0xks = Object.keys(_0xall).sort();
    const _0xstr = _0xks.map(_0xk => `${_0xk}=${_0xall[_0xk]}`).join('&');
    const _0xtk = _0xmd5(_0xstr);
    return { ..._0xp, token: _0xtk };
  },
  
  // 创建签名
  create(_0xparams: Record<string, string>) {
    return this._0xgen(_0xparams);
  }
};

// 模块导出
export default (function() {
  const _0xcore = _0xsig;
  return {
    sign: (_0xp: Record<string, string>) => _0xcore.create(_0xp)
  };
})();

